package com.robotserver.orders.jobs;

import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.service.TbOrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 淘宝结算订单同步JOB
 * @author jianghan
 * @date 2019-11-08 10:06
 */
@Component
public class TbSettleOrderSIdSyncJob {
    private static Logger logger = LoggerFactory.getLogger(TbSettleOrderSIdSyncJob.class);

    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private RedissonClient redissonClient;
    @Value("${cluster.group}")
    private String clusterGroup;

    /**
     * 每分钟执行一次拉取订单（时间范围是20分钟）
     */
//    @Scheduled(cron="0/30 * * * * ?")
    public void execute(){
        logger.debug("TbSettleOrderSIdSyncJob execute.");


        RLock lock = redissonClient.getLock(clusterGroup+"_service_tbsettle_order_sync_sid_lock");
        boolean getlock = false;
        try {
            if (getlock=lock.tryLock(0,1, TimeUnit.MINUTES)) {
                logger.debug("execute right.");
                Long tmptime = System.currentTimeMillis();
                Long end_time = tmptime / 1000;                     //截至时间为当前时间
                Long begin_time = (tmptime - 180000) / 1000;       //起始时间为前3分钟
                TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
                tbOrderSyncParam.setBegin_time(begin_time);
                tbOrderSyncParam.setEnd_time(end_time);
                //tbOrderSyncParam.setType(1);
                tbOrderSyncParam.setOrderScene(3l);
                tbOrderService.syncOrder(tbOrderSyncParam);
            }else {
                logger.debug("not execute right.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (getlock){
                lock.unlock();
                logger.debug("release lock.");
            }
        }
    }

    /**
     * lucher
     * 生产者-往redis队列当中生产alimmTokens
     * 每30秒执行一次查询联盟数据,然后保存到redis队列当中，充当生产者角色
     * @author lucher
     * @date 2021-03-09 10:06
     */
//    @Scheduled(cron="0/30 * * * * ?")
    public void scheduled_alimmTokens_producer(){
        logger.info("-----TbSettleOrderSIdSyncJob scheduled_alimmTokens_producer  start----");

        RLock lock = redissonClient.getLock("TbSettleOrderSIdSyncJob_scheduled_alimmTokens_producer");
        boolean getlock = false;
        try {
            if (getlock=lock.tryLock()) {
                logger.info("---- execute right----");
                Long tmptime = System.currentTimeMillis();
                Long end_time = tmptime / 1000;                     //截至时间为当前时间
                Long begin_time = (tmptime - 180000) / 1000;        //起始时间为前3分钟
                TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
                tbOrderSyncParam.setBegin_time(begin_time);
                tbOrderSyncParam.setEnd_time(end_time);
                //tbOrderSyncParam.setType(1);
                tbOrderSyncParam.setOrderScene(3l);  //会员订单
                tbOrderSyncParam.setRedisQueueKey(CacheKey.tbSettleOrderSldQueue);
                //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
                tbOrderService.syncOrder_producer(tbOrderSyncParam);
                logger.info("-------------往redis中推入淘宝联盟的会员账号的结算订单------tbOrderSyncParam=" + tbOrderSyncParam.toString());
            }else {
                logger.info("----not execute right----");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (getlock){
                lock.unlock();
                logger.info("----release lock----");
            }
        }
    }

}
